<?php

/**
 * Class trips_admin_module
 */
class trips_admin_module extends CI_Module
{

    private  $sid ;
    public function __construct()
    {
        parent::__construct();
        $this->sid = $GLOBALS['sid'];
        $this->load->model('admin.area_model');
        $this->load->model('trips.trips_model');
    }

    public function index()
    {
        $extra = "";
        $where = "status=0 ";
        $condition['subject'] = addslashes($this->input->get_post('subject'));
        $condition['only'] = addslashes($this->input->get_post('only'));
        if($condition['subject']){
            $where .= " and subject like '%".$condition['subject']."%' ";
        }
        if($condition['only']){
            $extra = 1 ;
        }
        $page_config['perpage']=20;   //每页条数
        $page_config['part']=2;//当前页前后链接数量
        $page_config['url']='trips/admin/index';//url
        $page_config['param'] = '?only='.$condition['only'].'&subject='.$condition['subject'];//参数
        $page_config['seg']=4;//参数取 index.php之后的段数，默认为3，即index.php/control/function/18 这种形式
        $page_config['nowindex'] = $this->uri->segment($page_config['seg']) ? $this->uri->segment($page_config['seg']):1;//当前页
        $page_config['total'] = $this->trips_model->trips_count($where,$extra);
        $list = $this->trips_model->trips_list($page_config['perpage'],$page_config['nowindex'],$where,$extra);

        $this->load->library('mypageclass');
        $this->mypageclass->initialize($page_config);

        foreach ($list as $k => $item)
        {
            $dist_str = array();
            foreach ($item['dist'] as $key => $dist)
            {
                $dist_str[]= $this->area_model->get_area_name($dist);
            }
            $list[$k]['dist_str'] = implode('<br>', $dist_str);
        }
        $this->data['list'] = $list ;
        $this->data['sid'] = $this->sid  ;
        $this->data['form_url'] = site_url('trips/admin/index');
        $this->data['condition'] = $condition;
        $this->load->view('admin/list', $this->data);
    }

    public function add()
    {
        if(strtolower($_SERVER['REQUEST_METHOD']) == 'post')
        {
            $_POST['t']['cruser'] = $this->session->userdata('admin_user_name');
            $_POST['t']['upuser'] = $this->session->userdata('admin_user_name');
            $_POST['t']['user_id']  = $this->session->userdata('admin_user_id');
            $_POST['t']['sid']  = $this->sid ;
            if($id = $this->trips_model->trips_add($_POST['t'])){

                $this->success('攻略添加成功!', site_url('trips/admin/index'), true);
            }else{
                $this->error('攻略添加失败!', '', true);
            }
        }
        $this->load->library('mytreeclass');
        $list = $this->trips_model->category_list();
        $this->mytreeclass->init($list);
        $str = "<option value='\$id' \$selected>\$spacer \$name</option>";
        $list = $this->mytreeclass->get_tree(0, $str);

        $this->load->view('admin/profile', array(
            'category_list' => $list
        ));
    }

    public function edit()
    {
        $id = $this->input->get('id');
        if(strtolower($_SERVER['REQUEST_METHOD']) == 'post')
        {
            $_POST['t']['upuser'] = $this->session->userdata('admin_user_name');
            if($this->trips_model->trips_edit($id, $_POST['t'])){
                $this->success('攻略修改成功!', site_url('trips/admin/index'), true);
            }else{
                $this->error('攻略修改失败!', '', true);
            }
        }

        $data = $this->trips_model->get_trips_by_id($id);
        $this->load->library('mytreeclass');
        $list = $this->trips_model->category_list();
        foreach ($list as $k => $v) {
            if($v['id'] == $data['category']){
                $list[$k]['selected'] = 'selected';
            }
        }

        $this->mytreeclass->init($list);
        $str = "<option value='\$id' \$selected>\$spacer \$name</option>";
        $list = $this->mytreeclass->get_tree(0, $str);
        $this->load->view('admin/profile_edit', array(
            'category_list' => $list,
            'data' => $data
        ));
    }

    public function del()
    {
        $id = $this->input->get('id');

        if($this->trips_model->trips_del($id)){
            $this->success('攻略删除成功!', site_url('trips/admin/index'), true);
        }else{
            $this->error('攻略删除失败!', '', true);
        }
    }

    public function category()
    {
        if(strtolower($_SERVER['REQUEST_METHOD']) == 'post')
        {
            $message = '操作成功。';
            /*增加分类*/
            if(!empty($_POST['new_category'])) {
                if ($this->trips_model->category_multi_add($_POST['new_category']))
                {
                    $message = '添加成功。';
                }
            }

            /*增加子类*/
            if(!empty($_POST['new_sub_category']))
            {
                if($this->trips_model->category_sub_multi_add($_POST['new_sub_category']))
                {
                    $message = '添加成功。';
                }
            }

            if(!empty($_POST['delete']))
            {
                if($this->trips_model->category_delete($_POST['delete']))
                {
                    $message .= '删除成功，不删除有攻略的分类。';
                }
            }

            $this->success($message, site_url('trips/admin/category'), TRUE);
        }

        $this->load->library('mytreeclass');

        $list = $this->trips_model->category_list();
        $this->mytreeclass->init($list);
        $list = $this->mytreeclass->get_tree_array(0, '');

        $retval = array();
        foreach ($list as $k => $v) {
            if($v['parent_id'] == 0){
                $retval[] = $v;
            }
            if($v['child'])
            {
                $retval = array_merge($retval, $v['child']);
            }
        }

        $this->load->view('admin/category', array('list' => $retval));
    }

    /**
     * 修改攻略
     */
    public function category_edit()
    {
        if(!empty($_POST['c']))
        {
            $row = $_POST['c'];
            if($this->trips_model->category_update($row))
            {
                $this->success('更新成功', site_url('trips/admin/category'), TRUE);
            }else{
                $this->success('更新失败', site_url('trips/admin/category_edit/?id='.$row['id']), TRUE);
            }
        }
        $id = $this->input->get('id');
        $retval = $this->trips_model->category_by_id($id);
        $this->load->view('admin/category_edit', array(
            'row' => $retval
        ));
    }
}